/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package symulator8051;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import parser8051.Interpreter8051;
import parser8051.ParseException;
import parser8051.Parser8051;

/**
 *
 * @author Wojtek
 */
public class GuiFrame extends javax.swing.JFrame {

	memory8051 mem = memory8051.getInstance();
	Interpreter8051 interpreter;
	int pc = 0;
	boolean running = false;

	/**
	 * Creates new form GuiFrame
	 */
	public GuiFrame() {
		initComponents();

		PrintStream ps = new PrintStream(new OutputStream() {
			@Override
			public void write(int b) throws IOException {
				jTextArea2.append("" + (char) b);
			}
		});

		System.setOut(ps);
		System.setErr(ps);

		initMemory();

		updateMemory();
	}

	public void initMemory() {

		// memory

		DefaultTableModel tm = (DefaultTableModel) jTable3.getModel();

		for (int i = 0; i < mem.mem.length; i++) {
			String hex = Integer.toHexString(i);
			String bin = Integer.toBinaryString(mem.get(i));

			if (hex.length() < 2) {
				hex = "0" + hex;
			}

			while (bin.length() < 8) {
				bin = "0" + bin;
			}

			tm.addRow(new Object[]{"0x" + hex, "0b" + bin + "   (" + mem.get(i) + ")"});
		}

		// special

		tm = (DefaultTableModel) jTable2.getModel();

		tm.addRow(new Object[]{"R0(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R0_1)) + "   (" + mem.get(mem.R0_1) + ")"});
		tm.addRow(new Object[]{"R1(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R1_1)) + "   (" + mem.get(mem.R1_1) + ")"});
		tm.addRow(new Object[]{"R2(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R2_1)) + "   (" + mem.get(mem.R2_1) + ")"});
		tm.addRow(new Object[]{"R3(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R3_1)) + "   (" + mem.get(mem.R3_1) + ")"});
		tm.addRow(new Object[]{"R4(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R4_1)) + "   (" + mem.get(mem.R4_1) + ")"});
		tm.addRow(new Object[]{"R5(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R5_1)) + "   (" + mem.get(mem.R5_1) + ")"});
		tm.addRow(new Object[]{"R6(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R6_1)) + "   (" + mem.get(mem.R6_1) + ")"});
		tm.addRow(new Object[]{"R7(b1)", "0b" + Integer.toBinaryString(mem.get(mem.R7_1)) + "   (" + mem.get(mem.R7_1) + ")"});
		tm.addRow(new Object[]{"R0(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R0_2)) + "   (" + mem.get(mem.R0_2) + ")"});
		tm.addRow(new Object[]{"R1(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R1_2)) + "   (" + mem.get(mem.R1_2) + ")"});
		tm.addRow(new Object[]{"R2(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R2_2)) + "   (" + mem.get(mem.R2_2) + ")"});
		tm.addRow(new Object[]{"R3(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R3_2)) + "   (" + mem.get(mem.R3_2) + ")"});
		tm.addRow(new Object[]{"R4(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R4_2)) + "   (" + mem.get(mem.R4_2) + ")"});
		tm.addRow(new Object[]{"R5(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R5_2)) + "   (" + mem.get(mem.R5_2) + ")"});
		tm.addRow(new Object[]{"R6(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R6_2)) + "   (" + mem.get(mem.R6_2) + ")"});
		tm.addRow(new Object[]{"R7(b2)", "0b" + Integer.toBinaryString(mem.get(mem.R7_2)) + "   (" + mem.get(mem.R7_2) + ")"});
		tm.addRow(new Object[]{"R0(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R0_3)) + "   (" + mem.get(mem.R0_3) + ")"});
		tm.addRow(new Object[]{"R1(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R1_3)) + "   (" + mem.get(mem.R1_3) + ")"});
		tm.addRow(new Object[]{"R2(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R2_3)) + "   (" + mem.get(mem.R2_3) + ")"});
		tm.addRow(new Object[]{"R3(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R3_3)) + "   (" + mem.get(mem.R3_3) + ")"});
		tm.addRow(new Object[]{"R4(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R4_3)) + "   (" + mem.get(mem.R4_3) + ")"});
		tm.addRow(new Object[]{"R5(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R5_3)) + "   (" + mem.get(mem.R5_3) + ")"});
		tm.addRow(new Object[]{"R6(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R6_3)) + "   (" + mem.get(mem.R6_3) + ")"});
		tm.addRow(new Object[]{"R7(b3)", "0b" + Integer.toBinaryString(mem.get(mem.R7_3)) + "   (" + mem.get(mem.R7_3) + ")"});
		tm.addRow(new Object[]{"R0(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R0_4)) + "   (" + mem.get(mem.R0_4) + ")"});
		tm.addRow(new Object[]{"R1(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R1_4)) + "   (" + mem.get(mem.R1_4) + ")"});
		tm.addRow(new Object[]{"R2(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R2_4)) + "   (" + mem.get(mem.R2_4) + ")"});
		tm.addRow(new Object[]{"R3(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R3_4)) + "   (" + mem.get(mem.R3_4) + ")"});
		tm.addRow(new Object[]{"R4(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R4_4)) + "   (" + mem.get(mem.R4_4) + ")"});
		tm.addRow(new Object[]{"R5(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R5_4)) + "   (" + mem.get(mem.R5_4) + ")"});
		tm.addRow(new Object[]{"R6(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R6_4)) + "   (" + mem.get(mem.R6_4) + ")"});
		tm.addRow(new Object[]{"R7(b4)", "0b" + Integer.toBinaryString(mem.get(mem.R7_4)) + "   (" + mem.get(mem.R7_4) + ")"});

		tm.addRow(new Object[]{"P0", "0b" + Integer.toBinaryString(mem.get(mem.P0)) + "   (" + mem.get(mem.P0) + ")"});
		tm.addRow(new Object[]{"P1", "0b" + Integer.toBinaryString(mem.get(mem.P1)) + "   (" + mem.get(mem.P1) + ")"});
		tm.addRow(new Object[]{"P2", "0b" + Integer.toBinaryString(mem.get(mem.P2)) + "   (" + mem.get(mem.P2) + ")"});
		tm.addRow(new Object[]{"P3", "0b" + Integer.toBinaryString(mem.get(mem.P3)) + "   (" + mem.get(mem.P3) + ")"});

		tm.addRow(new Object[]{"SP", "0b" + Integer.toBinaryString(mem.get(mem.SP)) + "   (" + mem.get(mem.SP) + ")"});
		tm.addRow(new Object[]{"DLP", "0b" + Integer.toBinaryString(mem.get(mem.DLP)) + "   (" + mem.get(mem.DLP) + ")"});
		tm.addRow(new Object[]{"DPH", "0b" + Integer.toBinaryString(mem.get(mem.DPH)) + "   (" + mem.get(mem.DPH) + ")"});
		tm.addRow(new Object[]{"DCON", "0b" + Integer.toBinaryString(mem.get(mem.DCON)) + "   (" + mem.get(mem.DCON) + ")"});
		tm.addRow(new Object[]{"TMOD", "0b" + Integer.toBinaryString(mem.get(mem.TMOD)) + "   (" + mem.get(mem.TMOD) + ")"});
		tm.addRow(new Object[]{"TL0", "0b" + Integer.toBinaryString(mem.get(mem.TL0)) + "   (" + mem.get(mem.TL0) + ")"});
		tm.addRow(new Object[]{"TL1", "0b" + Integer.toBinaryString(mem.get(mem.TL1)) + "   (" + mem.get(mem.TL1) + ")"});
		tm.addRow(new Object[]{"TH0", "0b" + Integer.toBinaryString(mem.get(mem.TH0)) + "   (" + mem.get(mem.TH0) + ")"});
		tm.addRow(new Object[]{"TH1", "0b" + Integer.toBinaryString(mem.get(mem.TH1)) + "   (" + mem.get(mem.TH1) + ")"});
		tm.addRow(new Object[]{"SCON", "0b" + Integer.toBinaryString(mem.get(mem.SCON)) + "   (" + mem.get(mem.SCON) + ")"});
		tm.addRow(new Object[]{"SBUF", "0b" + Integer.toBinaryString(mem.get(mem.SBUF)) + "   (" + mem.get(mem.SBUF) + ")"});
		tm.addRow(new Object[]{"IE", "0b" + Integer.toBinaryString(mem.get(mem.IE)) + "   (" + mem.get(mem.IE) + ")"});
		tm.addRow(new Object[]{"IP", "0b" + Integer.toBinaryString(mem.get(mem.IP)) + "   (" + mem.get(mem.IP) + ")"});
		tm.addRow(new Object[]{"PSW", "0b" + Integer.toBinaryString(mem.get(mem.PSW)) + "   (" + mem.get(mem.PSW) + ")"});
		tm.addRow(new Object[]{"ACC", "0b" + Integer.toBinaryString(mem.get(mem.ACC)) + "   (" + mem.get(mem.ACC) + ")"});
		tm.addRow(new Object[]{"B", "0b" + Integer.toBinaryString(mem.get(mem.B)) + "   (" + mem.get(mem.B) + ")"});
	}

	public void updateMemory() {

		//memory

		DefaultTableModel tm = (DefaultTableModel) jTable3.getModel();

		for (int i = 0; i < mem.mem.length; i++) {
			String bin = Integer.toBinaryString(mem.get(i));

			while (bin.length() < 8) {
				bin = "0" + bin;
			}

			tm.setValueAt("0b" + bin + "   (" + mem.get(i) + ")", i, 1);
		}

		// special
		
		tm = (DefaultTableModel) jTable2.getModel();
		
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R0_1)) + "   (" + mem.get(mem.R0_1) + ")", 0, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R1_1)) + "   (" + mem.get(mem.R1_1) + ")", 1, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R2_1)) + "   (" + mem.get(mem.R2_1) + ")", 2, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R3_1)) + "   (" + mem.get(mem.R3_1) + ")", 3, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R4_1)) + "   (" + mem.get(mem.R4_1) + ")", 4, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R5_1)) + "   (" + mem.get(mem.R5_1) + ")", 5, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R6_1)) + "   (" + mem.get(mem.R6_1) + ")", 6, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R7_1)) + "   (" + mem.get(mem.R7_1) + ")", 7, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R0_2)) + "   (" + mem.get(mem.R0_2) + ")", 8, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R1_2)) + "   (" + mem.get(mem.R1_2) + ")", 9, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R2_2)) + "   (" + mem.get(mem.R2_2) + ")", 10, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R3_2)) + "   (" + mem.get(mem.R3_2) + ")", 11, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R4_2)) + "   (" + mem.get(mem.R4_2) + ")", 12, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R5_2)) + "   (" + mem.get(mem.R5_2) + ")", 13, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R6_2)) + "   (" + mem.get(mem.R6_2) + ")", 14, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R7_2)) + "   (" + mem.get(mem.R7_2) + ")", 15, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R0_3)) + "   (" + mem.get(mem.R0_3) + ")", 16, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R1_3)) + "   (" + mem.get(mem.R1_3) + ")", 17, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R2_3)) + "   (" + mem.get(mem.R2_3) + ")", 18, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R3_3)) + "   (" + mem.get(mem.R3_3) + ")", 19, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R4_3)) + "   (" + mem.get(mem.R4_3) + ")", 20, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R5_3)) + "   (" + mem.get(mem.R5_3) + ")", 21, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R6_3)) + "   (" + mem.get(mem.R6_3) + ")", 22, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R7_3)) + "   (" + mem.get(mem.R7_3) + ")", 23, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R0_4)) + "   (" + mem.get(mem.R0_4) + ")", 24, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R1_4)) + "   (" + mem.get(mem.R1_4) + ")", 25, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R2_4)) + "   (" + mem.get(mem.R2_4) + ")", 26, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R3_4)) + "   (" + mem.get(mem.R3_4) + ")", 27, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R4_4)) + "   (" + mem.get(mem.R4_4) + ")", 28, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R5_4)) + "   (" + mem.get(mem.R5_4) + ")", 29, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R6_4)) + "   (" + mem.get(mem.R6_4) + ")", 30, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.R7_4)) + "   (" + mem.get(mem.R7_4) + ")", 31, 1);
		
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.P0)) + "   (" + mem.get(mem.P0) + ")", 32, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.P1)) + "   (" + mem.get(mem.P1) + ")", 33, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.P2)) + "   (" + mem.get(mem.P2) + ")", 34, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.P3)) + "   (" + mem.get(mem.P3) + ")", 35, 1);
		
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.SP)) + "   (" + mem.get(mem.SP) + ")", 36, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.DLP)) + "   (" + mem.get(mem.DLP) + ")", 37, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.DPH)) + "   (" + mem.get(mem.DPH) + ")", 38, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.DCON)) + "   (" + mem.get(mem.DCON) + ")", 39, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.TMOD)) + "   (" + mem.get(mem.TMOD) + ")", 40, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.TL0)) + "   (" + mem.get(mem.TL0) + ")", 41, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.TL1)) + "   (" + mem.get(mem.TL1) + ")", 42, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.TH0)) + "   (" + mem.get(mem.TH0) + ")", 43, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.TH1)) + "   (" + mem.get(mem.TH1) + ")", 44, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.SCON)) + "   (" + mem.get(mem.SCON) + ")", 45, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.SBUF)) + "   (" + mem.get(mem.SBUF) + ")", 46, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.IE)) + "   (" + mem.get(mem.IE) + ")", 47, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.IP)) + "   (" + mem.get(mem.IP) + ")", 48, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.PSW)) + "   (" + mem.get(mem.PSW) + ")", 49, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.ACC)) + "   (" + mem.get(mem.ACC) + ")", 50, 1);
		tm.setValueAt("0b" + Integer.toBinaryString(mem.get(mem.B)) + "   (" + mem.get(mem.B) + ")", 51, 1);

		// diodes p1

		String s = Integer.toBinaryString(mem.get(mem.R7_1));

		while (s.length() < 8) {
			s = "0" + s;
		}

		if (s.charAt(0) == '0') {
			jRadioButton1.setSelected(false);
		} else {
			jRadioButton1.setSelected(true);
		}

		if (s.charAt(1) == '0') {
			jRadioButton2.setSelected(false);
		} else {
			jRadioButton2.setSelected(true);
		}

		if (s.charAt(2) == '0') {
			jRadioButton3.setSelected(false);
		} else {
			jRadioButton3.setSelected(true);
		}

		if (s.charAt(3) == '0') {
			jRadioButton4.setSelected(false);
		} else {
			jRadioButton4.setSelected(true);
		}

		if (s.charAt(4) == '0') {
			jRadioButton5.setSelected(false);
		} else {
			jRadioButton5.setSelected(true);
		}

		if (s.charAt(5) == '0') {
			jRadioButton6.setSelected(false);
		} else {
			jRadioButton6.setSelected(true);
		}

		if (s.charAt(6) == '0') {
			jRadioButton7.setSelected(false);
		} else {
			jRadioButton7.setSelected(true);
		}

		if (s.charAt(7) == '0') {
			jRadioButton8.setSelected(false);
		} else {
			jRadioButton8.setSelected(true);
		}

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton4 = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        jButton5 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jRadioButton7 = new javax.swing.JRadioButton();
        jRadioButton8 = new javax.swing.JRadioButton();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Emulator 8051");
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Open...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Parse");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jButton4.setText("Run");
        jButton4.setEnabled(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jSlider1.setFont(new java.awt.Font("Tahoma", 1, 8)); // NOI18N
        jSlider1.setMajorTickSpacing(400);
        jSlider1.setMaximum(2000);
        jSlider1.setMinorTickSpacing(133);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setValue(500);
        jSlider1.setEnabled(false);

        jButton5.setText("Step");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jScrollPane2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jTable1.setFont(new java.awt.Font("Tahoma", 1, 8)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "PC", "INSTRUCTION", "ARG 1", "ARG 2"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(30);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(120);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jTextArea2.setColumns(300);
        jTextArea2.setFont(new java.awt.Font("Monospaced", 1, 9)); // NOI18N
        jTextArea2.setForeground(new java.awt.Color(255, 0, 0));
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jScrollPane3.setViewportView(jTextArea2);

        jLabel2.setText("Memory :");

        jTable2.setFont(new java.awt.Font("Lucida Console", 0, 12)); // NOI18N
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Reg", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(jTable2);
        jTable2.getColumnModel().getColumn(0).setResizable(false);
        jTable2.getColumnModel().getColumn(0).setPreferredWidth(10);
        jTable2.getColumnModel().getColumn(1).setResizable(false);
        jTable2.getColumnModel().getColumn(1).setPreferredWidth(150);

        jLabel1.setText("Special Registers :");

        jTable3.setFont(new java.awt.Font("Lucida Console", 0, 12)); // NOI18N
        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Adress", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(jTable3);
        jTable3.getColumnModel().getColumn(0).setResizable(false);
        jTable3.getColumnModel().getColumn(0).setPreferredWidth(10);
        jTable3.getColumnModel().getColumn(1).setResizable(false);
        jTable3.getColumnModel().getColumn(1).setPreferredWidth(150);

        jLabel3.setText("Register R7 (Diodes) :");

        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton6ActionPerformed(evt);
            }
        });

        jRadioButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton7ActionPerformed(evt);
            }
        });

        jRadioButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton8ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 88, Short.MAX_VALUE))
                    .addComponent(jSeparator3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton8)
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jRadioButton4)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jRadioButton1)
                                    .addComponent(jRadioButton2)
                                    .addComponent(jRadioButton3))
                                .addComponent(jRadioButton6, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jRadioButton7, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jRadioButton8, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jRadioButton5))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jSeparator1)
                    .addComponent(jSeparator2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3)))
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// guzik OPEN
		JFileChooser jfc = new JFileChooser();
		jfc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));

		int f = jfc.showOpenDialog(this);

		jTextArea1.setText("");

		try {
			FileReader r = new FileReader(jfc.getSelectedFile());
			BufferedReader br = new BufferedReader(r);

			String s = br.readLine();

			while (s != null) {
				jTextArea1.append(s + "\n");
				s = br.readLine();
			}
		} catch (FileNotFoundException ex) {
			Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);
		}

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
		// guzik PARSE

		jTextArea2.setText("");

		if (jTextArea1.getText().equals("") || jTextArea1.getText() == null) {
			JOptionPane.showMessageDialog(null, "Editor is empty!", "Error", JOptionPane.ERROR_MESSAGE);
		}

		StringReader reader = new StringReader(jTextArea1.getText());
		Parser8051 parser = new Parser8051(reader);
		interpreter = new Interpreter8051();

		try {
			parser.Start(interpreter);
		} catch (ParseException ex) {
			Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);
		}

		int pc = 0;

		DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
		int rc = tm.getRowCount();
		for (int i = 0; i < rc; i++) {
			tm.removeRow(0);
		}

		for (parser8051.Instruction i : interpreter.instructionList) {
			if (i.arguments != null) {
				switch (i.arguments.length) {
					case 1:
						tm.addRow(new Object[]{pc, i.name, i.arguments[0]});
						break;
					case 2:
						tm.addRow(new Object[]{pc, i.name, i.arguments[0], i.arguments[1]});
						break;
				}
			} else {
				tm.addRow(new Object[]{pc, i.name});
			}

			pc++;
		}
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
		// RUN
		
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
		// TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
		// TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
		// TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton5ActionPerformed

    private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton6ActionPerformed
		// TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton6ActionPerformed

    private void jRadioButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton7ActionPerformed
		// TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton7ActionPerformed

    private void jRadioButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton8ActionPerformed
		// TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton8ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

		//STEP

		int row = interpreter.performNextInstruction();
		jTable1.setRowSelectionInterval(row, row);
		updateMemory();
    }//GEN-LAST:event_jButton5ActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Windows".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(GuiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(GuiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(GuiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(GuiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new GuiFrame().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration//GEN-END:variables
}
